package com.health.controller;

import com.health.common.PageBean;
import com.health.common.PageQueryDTO;
import com.health.common.Result;
import com.health.pojo.CheckGroup;
import com.health.pojo.Checkitem;
import com.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.PerformanceSensitive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhangzhen
 * @version 1.0
 * @date 2025/1/18 22:50
 */
/*checkgroup/findPage*/
@Slf4j
@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    //新增检查组
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup, @RequestParam Integer[] checkitemIds) {
        try {
            checkGroupService.add(checkGroup, checkitemIds);
        } catch (Exception e) {
            return Result.error("添加失败");
        }
        return Result.success();
    }

    @PostMapping("/findPage")
    public PageBean findPage(@RequestBody PageQueryDTO pageQueryDTO) {
        log.info("pageQueryDTO:{}", pageQueryDTO);
        PageBean page = checkGroupService.findPage(pageQueryDTO);

        return page;
    }
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        log.info("id:{}",id);
        CheckGroup byId = checkGroupService.findById(id);
        return Result.success(byId);
    }
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam Integer id){
        log.info("id:{}",id);
        List<Integer> checkitemIds= checkGroupService.findByAndId(id);
        return Result.success(checkitemIds);
    }
    //编辑检查组信息，同时需要关联检查项
    @PostMapping("/edit")
    public Result  edit(@RequestBody CheckGroup checkGroup,@RequestParam Integer[] checkitemIds){

           log.info("checkGroup.id:{},checkitemIs:{}",checkGroup.getId(),checkitemIds);
        checkGroupService.edit(checkGroup,checkitemIds);
        return Result.success();

    }
    @GetMapping("/delete")
    public Result deleteById(@RequestParam Integer id){
        log.info("id:{}",id);
        checkGroupService.deleteById(id);
        return  Result.success();
    }
    @GetMapping("/findAll")
    public Result findAll(){
       List<CheckGroup> checkGroups= checkGroupService.findAll();
       return Result.success(checkGroups);
    }

}